
use core::marker::PhantomData;

use embedded_hal::digital::OutputPin;

/// 共阳类型
pub struct CommomAnode;

pub struct CommomCathod;


#[allow(non_camel_case_types)]
pub struct RGB_LED<R, G, B, Commom> {
    pin_r: R,
    pin_g: G,
    pin_b: B,
    _com: PhantomData<Commom>,
}


impl<R, G, B, Commom> RGB_LED<R,G,B,Commom> {
    pub fn new(r: R, g: G, b: B) -> Self {
        Self {
            pin_r: r,
            pin_b: b,
            pin_g: g,
            _com: PhantomData,
        }
    }
}


macro_rules! rgb_led_ops {
    ($com: ident, $on: ident, $off: ident) => {
        impl<R, G, B> RGB_LED<R, G, B, $com> 
            where R: OutputPin,
                G: OutputPin,
                B: OutputPin,
        {
            pub fn turn_on_red(&mut self) -> &mut Self {
                self.pin_r.$on().unwrap();
                self
            }

            pub fn turn_on_blue(&mut self) -> &mut Self {
                self.pin_b.$on().unwrap();

                self
            }

            pub fn turn_on_green(&mut self) -> &mut Self {
                self.pin_g.$on().unwrap();

                self
            }

            pub fn turn_off_red(&mut self) -> &mut Self {
                self.pin_r.$off().unwrap();
                self
            }


            pub fn turn_off_blue(&mut self) -> &mut Self {
                self.pin_b.$off().unwrap();
                self
            }

            pub fn turn_off_green(&mut self) -> &mut Self {
                self.pin_g.$off().unwrap();
                self
            }

            pub fn turn_off(&mut self) -> &mut Self {
                self.pin_r.$off().unwrap();
                self.pin_g.$off().unwrap();
                self.pin_b.$off().unwrap();
                self
            }

            pub fn turn_on(&mut self) -> &mut Self  {
                self.pin_r.$on().unwrap();
                self.pin_g.$on().unwrap();
                self.pin_b.$on().unwrap();
                self
            }

            pub fn only_red(&mut self) -> &mut Self  {
                self.pin_r.$on().unwrap();
                self.pin_g.$off().unwrap();
                self.pin_b.$off().unwrap();
                self
            }

            pub fn only_blue(&mut self) -> &mut Self  {
                self.pin_r.$off().unwrap();
                self.pin_g.$off().unwrap();
                self.pin_b.$on().unwrap();
                self
            }

            pub fn only_green(&mut self) -> &mut Self  {
                self.pin_r.$off().unwrap();
                self.pin_g.$on().unwrap();
                self.pin_b.$off().unwrap();
                self
            }
        }
    };
}


rgb_led_ops!(CommomAnode, set_low, set_high);
rgb_led_ops!(CommomCathod, set_high, set_low);




